<template>
	<view>
		<view class="box">
			<view class="menu_content_box">
				<view class="pt35 tac">
					<image src="/static/logo.png" class="logo_size"></image> 		</view>
				<view class="mt25 fz30 cor_000 tac">{{appname}}</view>
				<view class="mt25 fz30 cor_000 tac">当前版本：Version {{version}}</view>
			<!-- 	<view class="mt25 fz30 cor_000 tac">最新版本：Version {{server_version}}</view> -->
				<view class="mt50 version_update_btn aic jcc" @tap="getVersion">版本更新</view>

				<view class="service_agreement_fz">
					<view class="fz28 cor_007" @tap="toAgreement">{{appname}}软件许可及服务协议</view>
					<view class="mt15 cor_007" @tap="toPrivacy">《隐私政策》</view>
					<view class="mt25 fz22 cor_999">{{copyright}}</view>
					<view class="mt10 fz22 cor_999">{{copyrightinfo}}</view>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import {
		toast,
		showLoading,
		hideLoading
	} from '@/utils/toast.js';
	import {
		getVersionApi
	} from '@/http/api.js';
	
	export default {
		data() {
			return {
				version: '1.0.0',
				server_version: '1.0.1',
				appname: '挖宝',
				copyright: '合肥松创工程机械有限公司 版权所有',
				copyrightinfo: 'copyright @ 2021',
				versioncode: '1',
				downloadUrl: 'https://download.pc6.com/down/931021/',
				beNeedUpdate: false,
				disabled: false
			}
		},
		onLoad() {
			this.getVersion();
		},
		methods: {
			toAgreement() {
				uni.navigateTo({
					url: '/pages/agreement/statement'
				})
			},

			toPrivacy() {
				uni.navigateTo({
					url: '/pages/agreement/privacy'
				})
			},
			
			getVersion() {
				if (this.disabled) {
					toast('APP下载中，请勿重复点击！');
					return false;
				}
				let that = this;
				getVersionApi({
					version: that.version
				}).then(res=>{
					that.server_version = res.data.latestVer;
					that.beNeedUpdate = res.data.beNeedUpdate;
					that.downloadUrl = res.data.downloadUrl;
					if (res.data.beNeedUpdate) {
						that.AndroidCheckUpdate();
					} else {
						toast('已经是最新版啦');
					}
				}).catch(err=>{
					
				})
			},

			/**
			 * 安卓应用的检测更新实现
			 */
			AndroidCheckUpdate: function() {
				let that = this;
				uni.showModal({
					title: "版本更新",
					content: '有新的版本发布，是否立即进行新版本下载？',
					confirmText: '立即更新',
					cancelText: '稍后进行',
					success: function(res) {
						if (res.confirm) {
							that.disabled = true;
							uni.showToast({
								icon: "none",
								mask: true,
								title: '有新的版本发布，程序已启动自动更新。新版本下载完成后将自动弹出安装程序',
								duration: 5000,
							});
							//设置 最新版本apk的下载链接
							var dtask = plus.downloader.createDownload(that.downloadUrl, {},
								function(d, status) {
									// 下载完成  
									if (status == 200) {
										plus.runtime.install(plus.io.convertLocalFileSystemURL(
											d.filename), {}, {}, function(error) {
											uni.showToast({
												title: '安装失败',
												duration: 1500
											});
										})
									} else {
										uni.showToast({
											title: '更新失败',
											duration: 1500
										});
									}
								});
							dtask.start();
						} else if (res.cancel) {
							console.log('稍后更新');
						}
					}
				});
				
			}
		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}

	.menu_content_box {
		position: relative;
		height: 100vh;
		background-color: #fff;
	}

	.logo_size {
		width: 200rpx;
		height: 200rpx;
	}

	.version_update_btn {
		display: flex;
		width: 180rpx;
		height: 64rpx;
		font-size: 30rpx;
		color: #333;
		background-color: #fff;
		border: 2rpx solid #000;
		border-radius: 15rpx;
		margin-left: auto;
		margin-right: auto;
	}

	.service_agreement_fz {
		position: absolute;
		left: 0;
		bottom: 35rpx;
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		z-index: 2;
	}
</style>
